requirejs(['js/three.min.js', 'js/Core.js'], function() {
	requirejs(['js/Utils/Detector.js', 'js/Utils/OrbitControl.js', 'js/Classes/Scenes/Title.js'], function() {
		//Game.scene = new THREE.Scene();
		Game.camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 10, 100000);
		Game.clock = new THREE.Clock();

		if (Detector.webgl)
			Game.renderer = new THREE.WebGLRenderer({
				antialias : true
			});
		else
			Game.renderer = new THREE.CanvasRenderer();

		Game.renderer.setSize(window.innerWidth, window.innerHeight);
		document.body.appendChild(Game.renderer.domElement);

		if (Game.debug.orbitControls === true)
			Game.orbitControls = new THREE.OrbitControls(Game.camera, Game.renderer.domElement);

		Game.scene = new Classes.Scenes.TitleSplashScreen();

		Game.render = function() {
			var delta = Game.clock.getDelta();

			requestAnimationFrame(Game.render);
			Game.renderer.render(Game.scene, Game.camera);

			for (var key in Game.scene.Components) {
				var element = Game.scene.Components[key];

				if (element.gravitySensitive) {
					element.position.y += element.gravityForce * delta;
					element.gravityForce += -9.81 * delta;
					//continue;
					var originPoint = element.position.clone();

					for (var vertexIndex = 0; vertexIndex < element.geometry.vertices.length; vertexIndex++) {
						var localVertex = element.geometry.vertices[vertexIndex].clone();
						var globalVertex = localVertex.applyMatrix4(element.matrix);
						var directionVector = globalVertex.sub(element.position);

						var ray = new THREE.RaycastJe meer(originPoint, directionVector.clone().normalize());
						var collisionResults = ray.intersectObjects(Game.scene.Components);
						if (collisionResults.length > 0 && collisionResults[0].distance < directionVector.length()) {
							element.gravityForce = 0;
						}
					}
				}
			}

			if (Game.debug.orbitControls === true)
				Game.orbitControls.update();
		};

		Game.render();

		Game.scene.start();

		/* var planeGeometry = new THREE.PlaneGeometry(400, 400, 1, 1);
		 var planeMaterial = new THREE.MeshLambertMaterial({
		 color : 0x00ff00
		 });
		 var plane = new THREE.Mesh(planeGeometry, planeMaterial);
		 plane.rotation.x = THREE.Math.degToRad(-90);
		 plane.receiveShadow = true;
		 scene.add(plane);

		 var geometry = new THREE.PlaneGeometry(100, 100, 1, 1);
		 var kirbyTexture = THREE.ImageUtils.loadTexture('Kirby.png');
		 var animator = new TextureAnimator(kirbyTexture, 12, 1, 11, 70);
		 var moonMaterial = new THREE.MeshLambertMaterial({
		 map : kirbyTexture,
		 side : 2,
		 transparent : true
		 });
		 var cube = new THREE.Mesh(geometry, moonMaterial);
		 cube.position.x = 0;
		 cube.position.y = 35;
		 cube.position.z = 0;
		 cube.receiveShadow = true;
		 scene.add(cube);

		 camera.position.set(100, 100, 300);
		 camera.lookAt(new THREE.Vector3(0, 0, 0)); */
	});
});

function TextureAnimator(texture, tilesHoriz, tilesVert, numTiles, tileDispDuration) {
	// note: texture passed by reference, will be updated by the update function.

	this.tilesHorizontal = tilesHoriz;
	this.tilesVertical = tilesVert;
	// how many images does this spritesheet contain?
	//  usually equals tilesHoriz * tilesVert, but not necessarily,
	//  if there at blank tiles at the bottom of the spritesheet.
	this.numberOfTiles = numTiles;
	texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
	texture.repeat.set(1 / this.tilesHorizontal, 1 / this.tilesVertical);

	// how long should each image be displayed?
	this.tileDisplayDuration = tileDispDuration;

	// how long has the current image been displayed?
	this.currentDisplayTime = 0;

	// which image is currently being displayed?
	this.currentTile = 0;

	this.update = function(milliSec) {
		this.currentDisplayTime += milliSec;
		while (this.currentDisplayTime > this.tileDisplayDuration) {
			this.currentDisplayTime -= this.tileDisplayDuration;
			this.currentTile++;
			if (this.currentTile == this.numberOfTiles)
				this.currentTile = 0;
			var currentColumn = this.currentTile % this.tilesHorizontal;
			texture.offset.x = currentColumn / this.tilesHorizontal;
			var currentRow = Math.floor(this.currentTile / this.tilesHorizontal);
			texture.offset.y = currentRow / this.tilesVertical;
		}
	};
}
